package com.huicoo.forestmanager.ui.patrol.forestCampTask.presenter;


import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.huicoo.forestmanager.network.bean.patrol.GetMyForestZoneBean;
import com.huicoo.forestmanager.network.bean.patrol.GetReportTimeSpanBean;
import com.huicoo.forestmanager.network.bean.patrol.PatrolTaskReportPositionBean;
import com.huicoo.forestmanager.network.bean.patrol.RecorddetailsBean;
import com.huicoo.forestmanager.network.bean.patrol.ReportPointEntity;
import com.huicoo.forestmanager.network.bean.patrol.StartPatrolBean;
import com.huicoo.forestmanager.network.bean.patrol.StopPatrolBean;
import com.huicoo.forestmanager.ui.patrol.forestCampTask.contract.ForestCampTaskContract;
import com.huicoo.forestmanager.ui.patrol.forestCampTask.model.ForestCampTaskModel;
import com.huicoo.forestmanager.util.JsonUtils;
import com.huicoo.forestmanager.util.LogUtils;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2019/4/8		    Administrator
 * 进行巡查任务的presenter
 */
public class ForestCampTaskPresenter implements ForestCampTaskContract.Presenter {
    private ForestCampTaskContract.View view;
    private ForestCampTaskContract.Model model;

    public ForestCampTaskPresenter(ForestCampTaskContract.View view) {
        this.view = view;
        this.model = new ForestCampTaskModel();
    }

    @Override
    public void getMyForestZone(HashMap<String, String> params, boolean isOnlyRefreshEvent) {
        Call<GetMyForestZoneBean> call = model.getMyForestZone(params);
        call.enqueue(new Callback<GetMyForestZoneBean>() {
            @Override
            public void onResponse(@NonNull Call<GetMyForestZoneBean> call, @NonNull Response<GetMyForestZoneBean> response) {
                try {
                    //数据请求成功
                    LogUtils.v("gogogo get my forest zone result = " + JsonUtils.toJson(response.body()));
                    if (response.body() == null) {
                        //数据请求失败
                        if (view != null) {
                            view.fail("请求结果为空");
                        }
                        return;
                    }
                    if (TextUtils.equals(response.body().getCode(), "200")) {
                        GetMyForestZoneBean.GetMyForestZoneData Data = response.body().getData();
                        if (view != null) {
                            if (!isOnlyRefreshEvent) {
                                view.getMyForestZoneSuccess(Data);
                            }else{
                                view.refreshEvent(Data.EventInfos);
                            }
                        }
                    } else {
                        if (view != null) {
                            view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(@NonNull Call<GetMyForestZoneBean> call, @NonNull Throwable t) {
                //数据请求失败
                t.printStackTrace();
                if (view != null) {
                    view.fail("请求失败，请重试");
                }
            }
        });
    }

    @Override
    public void getTaskDetails(HashMap<String, String> params) {
        Call<RecorddetailsBean> call = model.getTaskDetails(params);
        call.enqueue(new Callback<RecorddetailsBean>() {
            @Override
            public void onResponse(@NonNull Call<RecorddetailsBean> call, @NonNull Response<RecorddetailsBean> response) {
                try {
                    //数据请求成功
                    LogUtils.v("gogogo get task details result = " + JsonUtils.toJson(response.body()));
                    if (response.body() == null) {
                        //数据请求失败
                        if (view != null) {
                            view.fail("请求结果为空");
                        }
                        return;
                    }
                    if (TextUtils.equals(response.body().getCode(), "200")) {
                        RecorddetailsBean.Data Data = response.body().getData();
                        if (view != null) {
                            view.getTaskDetailsSuccess(Data);
                        }
                    } else {
                        if (view != null) {
                            view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(@NonNull Call<RecorddetailsBean> call, @NonNull Throwable t) {
                //数据请求失败
                t.printStackTrace();
                if (view != null) {
                    view.fail("请求失败，请重试");
                }
            }
        });
    }

    @Override
    public void getReportTimeSpan(HashMap<String, String> params) {
        Call<GetReportTimeSpanBean> call = model.getReportTimeSpan(params);
        call.enqueue(new Callback<GetReportTimeSpanBean>() {
            @Override
            public void onResponse(@NonNull Call<GetReportTimeSpanBean> call, @NonNull Response<GetReportTimeSpanBean> response) {
                try {
                    //数据请求成功
                    LogUtils.v("gogogo get task report time span result = " + JsonUtils.toJson(response.body()));
                    if (response.body() == null) {
                        //数据请求失败
                        if (view != null) {
                            view.fail("请求结果为空");
                        }
                        return;
                    }
                    if (TextUtils.equals(response.body().getCode(), "200")) {
                        if (view != null) {
                            view.getReportTimeSpan(response.body().getData().ReportTimeSpan, response.body().getData().Tolerance, response.body().getData().MaxTolerance);
                        }
                    } else {
                        if (view != null) {
                            view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(@NonNull Call<GetReportTimeSpanBean> call, @NonNull Throwable t) {
                //数据请求失败
                t.printStackTrace();
                if (view != null) {
                    view.fail("请求失败，请重试");
                }
            }
        });
    }

    @Override
    public void startTask(HashMap<String, String> params) {
        Call<StartPatrolBean> call = model.startTask(params);
        call.enqueue(new Callback<StartPatrolBean>() {
            @Override
            public void onResponse(@NonNull Call<StartPatrolBean> call, @NonNull Response<StartPatrolBean> response) {
                try {
                    //数据请求成功
                    LogUtils.v("gogogo patrol start task result = " + JsonUtils.toJson(response.body()));
                    if (response.body() == null) {
                        //数据请求失败
                        if (view != null) {
                            view.fail("请求结果为空");
                        }
                        return;
                    }
                    if (TextUtils.equals(response.body().getCode(), "200")) {
                        StartPatrolBean.StartPatrolData Data = response.body().getData();
                        if (view != null) {
                            view.startTaskSuccess(Data);
                        }
                    } else {
                        if (view != null) {
                            view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(@NonNull Call<StartPatrolBean> call, @NonNull Throwable t) {
                //数据请求失败
                t.printStackTrace();
                if (view != null) {
                    view.fail("请求失败，请重试");
                }
            }
        });
    }

    @Override
    public void reportPosition(HashMap<String, String> params, ReportPointEntity entity) {
        Call<PatrolTaskReportPositionBean> call = model.reportPosition(params);
        call.enqueue(new Callback<PatrolTaskReportPositionBean>() {
            @Override
            public void onResponse(@NonNull Call<PatrolTaskReportPositionBean> call, @NonNull Response<PatrolTaskReportPositionBean> response) {
                try {
                    //数据请求成功
                    LogUtils.v("gogogo patrol task report position result = " + JsonUtils.toJson(response.body()));
                    if (response.body() == null) {
                        //数据请求失败
                        if (view != null) {
                            view.fail("请求结果为空");
                        }
                        return;
                    }
                    if (TextUtils.equals(response.body().getCode(), "200")) {
                        PatrolTaskReportPositionBean.PatrolTaskReportPositionData Data = response.body().getData();
                        if (Data != null && Data.IsSuccess) {
                            if (view != null) {
                                view.reportPositionSuccess(entity);
                            }
                        } else {
                            if (view != null) {
                                view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                            }
                        }
                    } else {
                        if (view != null) {
                            view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(@NonNull Call<PatrolTaskReportPositionBean> call, @NonNull Throwable t) {
                //数据请求失败
                t.printStackTrace();
//                view.fail("请求失败，请重试");
            }
        });
    }

    @Override
    public void stopTask(HashMap<String, String> params, boolean toReportRecord) {
        Call<StopPatrolBean> call = model.stopTask(params);
        call.enqueue(new Callback<StopPatrolBean>() {
            @Override
            public void onResponse(@NonNull Call<StopPatrolBean> call, @NonNull Response<StopPatrolBean> response) {
                try {
                    //数据请求成功
                    LogUtils.v("gogogo patrol stop task result = " + JsonUtils.toJson(response.body()));
                    if (response.body() == null) {
                        //数据请求失败
                        if (view != null) {
                            view.fail("请求结果为空");
                        }
                        return;
                    }
                    if (TextUtils.equals(response.body().getCode(), "200")) {
                        StopPatrolBean.StopPatrolData Data = response.body().getData();
                        if (Data != null && Data.IsSuccess) {
                            if (view != null) {
                                view.stopTaskSuccess(toReportRecord);
                            }
                        } else {
                            if (view != null) {
                                view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                            }
                        }
                    } else {
                        if (view != null) {
                            view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(@NonNull Call<StopPatrolBean> call, @NonNull Throwable t) {
                //数据请求失败
                t.printStackTrace();
                if (view != null) {
                    view.fail("请求失败，请重试");
                }
            }
        });
    }

    @Override
    public void destroy() {
        view = null;
    }

}
